/*
 * JSF Toolkit Component Framework
 * Copyright (C) 2007 Noah Sloan <iamnoah A-T gmail D0T com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 */
package com.jsftoolkit.skinning;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.jsftoolkit.utils.ResourceFilterImpl;

/**
 * Used as a managed bean to load the component skin definition.
 * 
 * @see ResourceFilterImpl
 * @author noah
 * 
 */
public class SkinMap extends HashMap<String, String> {

	private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
			.getLog(SkinMap.class);

	private static final long serialVersionUID = -9130888405244577402L;

	private String resource;

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
		load();
	}

	@SuppressWarnings("unchecked")
	public void load() {
		InputStream r = getClass().getResourceAsStream(resource);
		if (r == null) {
			r = getClass().getResourceAsStream(
					"/com/jsftoolkit/skinning/empty.skin.properties");
		}
		if (r != null) {
			Properties props = new Properties();
			try {
				props.load(r);
				putAll((Map) props);
			} catch (IOException e) {
				LOG.debug("Error loading skin.", e);
			}
		}
	}
}
